//
//  FirstViewController.m
//  dingding
//
//  Created by dingding on 15/12/2.
//  Copyright © 2015年 dingding. All rights reserved.
//

#import "ShareHelper.h"
#import "UIView+Activity.h"
#import "UIImageHelper.h"
#import <Social/SLComposeViewController.h>
#import "ViewUtils.h"
#import "StringUtil.h"

@interface ShareHelper ()
@end

@implementation ShareHelper

//从10.3.3开始分享开始使用弹出式
+(BOOL)isNewSystem
{
    NSString *systemVersion = [UIDevice currentDevice].systemVersion;
    NSComparisonResult comparisonResult = [systemVersion compare:@"10.3.3" options:NSNumericSearch];
    if (comparisonResult != NSOrderedAscending) {
        return YES;
    } else {
        return NO;
    }
}

+(void)shareImgs:(int)flag imgs:(NSArray*)imgs txt:(NSString*)txt type:(NSString*)type vc:(UIViewController*)VC okfunc:(void (^)(void))func
{
    [UIImageHelper downloadThenUpdateUI:imgs complete:^(NSArray *uiimages) {
        if([ShareHelper isNewSystem]){
            UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:uiimages applicationActivities:nil];
            controller.excludedActivityTypes = @[UIActivityTypePostToFacebook,
                                           UIActivityTypePostToTwitter,
                                           UIActivityTypeMail,
                                           UIActivityTypePrint,
                                           UIActivityTypeCopyToPasteboard,
                                           UIActivityTypeAssignToContact,
                                           UIActivityTypeSaveToCameraRoll,
                                           UIActivityTypeAddToReadingList,
                                           UIActivityTypePostToFlickr,
                                           UIActivityTypePostToVimeo,
                                           UIActivityTypePostToTencentWeibo,
                                           UIActivityTypeAirDrop,
                                           UIActivityTypeOpenInIBooks];
            if ([controller respondsToSelector:@selector(popoverPresentationController)]) {
                controller.popoverPresentationController.sourceView = VC.view;
            }
            [VC presentViewController:controller animated:YES completion:nil];
        }else{
            NSString *test = @"com.tencent.xin.sharetimeline";
            // 2.创建分享的控制器
            SLComposeViewController *composeVc = [SLComposeViewController composeViewControllerForServiceType:test];
            if (composeVc == nil){
                [ViewUtils show1Alert:nil content:@"亲，你还没安装微信哦" ok:@"我知道啦"];
                return;
            }
            if(txt){
                [composeVc setInitialText:txt];
            }
            // 2.2.添加一个图片
            for(int i=0;i<uiimages.count;i++){
                [composeVc addImage:uiimages[i]];
            }
            if ([composeVc respondsToSelector:@selector(popoverPresentationController)]) {
                composeVc.popoverPresentationController.sourceView = VC.view;
            }
            // 3.弹出分享控制器（以Modal形式弹出）
            [VC presentViewController:composeVc animated:YES completion:nil];
            // 4.监听用户点击了取消还是发送
            /*
             SLComposeViewControllerResultCancelled,
             SLComposeViewControllerResultDone
             */
            composeVc.completionHandler = ^(SLComposeViewControllerResult result){
                if (result == SLComposeViewControllerResultCancelled) {
                    NSLog(@"点击了取消");
                }else if(result ==SLComposeViewControllerResultDone)
                {
                    if(func){
                        func();
                    }
                } else{
                    
                }
            };
        }
        if(txt){
           [StringUtil copyTxt:txt];
        }
    }];
}

+(void)shareArticle: (NSString *)title image:(UIImage *)image url:(NSURL *)url type:(NSString *)type vc:(UIViewController *)VC okfunc:(void (^)(void))func
{
    if([ShareHelper isNewSystem]){
        NSArray * activityItems = @[title,
                                    image,
                                    url];
        UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
        /*controller.excludedActivityTypes = @[UIActivityTypePostToFacebook,
         UIActivityTypePostToTwitter,
         UIActivityTypePostToWeibo,
         UIActivityTypeMail,
         UIActivityTypeMessage,
         UIActivityTypePrint,
         UIActivityTypeCopyToPasteboard,
         UIActivityTypeAssignToContact,
         UIActivityTypeSaveToCameraRoll,
         UIActivityTypeAddToReadingList,
         UIActivityTypePostToFlickr,
         UIActivityTypePostToVimeo,
         UIActivityTypePostToTencentWeibo,
         UIActivityTypeAirDrop,
         UIActivityTypeOpenInIBooks];*/
        NSMutableArray *excludeTypesM =  [NSMutableArray arrayWithArray:@[UIActivityTypePostToFacebook,
                                                                          UIActivityTypePostToTwitter,
                                                                          UIActivityTypePostToWeibo,
                                                                          UIActivityTypeMail,
                                                                          UIActivityTypeMessage,
                                                                          UIActivityTypePrint,
                                                                          UIActivityTypeCopyToPasteboard,
                                                                          UIActivityTypeAssignToContact,
                                                                          UIActivityTypeSaveToCameraRoll,
                                                                          UIActivityTypeAddToReadingList,
                                                                          UIActivityTypePostToFlickr,
                                                                          UIActivityTypePostToVimeo,
                                                                          UIActivityTypePostToTencentWeibo,
                                                                          UIActivityTypeAirDrop]];
        if ([[UIDevice currentDevice].systemVersion floatValue] >= 9.0) {
            [excludeTypesM addObject:UIActivityTypeOpenInIBooks];
        }
        if ([[UIDevice currentDevice].systemVersion floatValue] >= 11.0) {
            if (@available(iOS 11.0, *)) {
                [excludeTypesM addObject:UIActivityTypeMarkupAsPDF];
            } else {
                // Fallback on earlier versions
            }
        }
        controller.excludedActivityTypes = excludeTypesM;
        
        if ([controller respondsToSelector:@selector(popoverPresentationController)]) {
            controller.popoverPresentationController.sourceView = VC.view;
        }
        [VC presentViewController:controller animated:YES completion:nil];
    }else{
        NSString *test = @"com.tencent.xin.sharetimeline";
        // 2.创建分享的控制器
        SLComposeViewController *composeVc = [SLComposeViewController composeViewControllerForServiceType:test];
        if (composeVc == nil){
            [ViewUtils show1Alert:nil content:@"亲，你还没安装微信哦" ok:@"我知道啦"];
            return;
        }
        
        // 图片
        [composeVc addImage: image];
        
        // 网址
        [composeVc addURL: url];
        
        // 文字
        if(title){
            [composeVc setInitialText: title];
        }
        
        if ([composeVc respondsToSelector:@selector(popoverPresentationController)]) {
            composeVc.popoverPresentationController.sourceView = VC.view;
        }
        // 3.弹出分享控制器（以Modal形式弹出）
        [VC presentViewController:composeVc animated:YES completion:nil];
        // 4.监听用户点击了取消还是发送
        /*
         SLComposeViewControllerResultCancelled,
         SLComposeViewControllerResultDone
         */
        composeVc.completionHandler = ^(SLComposeViewControllerResult result){
            if (result == SLComposeViewControllerResultCancelled) {
                
            }else if(result ==SLComposeViewControllerResultDone)
            {
                if(func){
                    func();
                }
            } else{
                
            }
        };
    }
    if(title){
        [StringUtil copyTxt:title];
    }
}

@end
